#Copyright Tyler Darnell

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

import random

class Resc:
    #an actual resource
    #used in world class to define need for top level resources.
    #also used in business classes to determine prices
    demand = 0 #number between 1 and 1200
    type = ""
    rarity = 0 #number between 1 & 100, higher is more rare, thus more demand
    price = 0 #price per ton

    def __init__(self,name,rare):
        self.type = name
        self.rarity = rare

    def getNeed(self):
        #algorithm that determines demand, thus determines price down the calculation train
        #simple for now
        k = 12 #constant, don't ask why I chose it, likely to change. Based off commodity market data and the maelstrom known as my mind
        rnd = random.randrange(1,4) #get the RANDOM FACTOR-TOR-TOR
        self.demand = (k * self.rarity) / rnd #maixmum from this is 1200, hence the demand numbers
        print "Current demand: ", self.demand
        
    def getPrice(self):
        rnd = random.randrange(1, 9) #another factor to add dynamic-ism
        self.price = self.demand / rnd
        print "current price: ", self.price

class RescVein:
    type = Resc("tmp",-1) #generic vein of resources
    size = 0 #between 0 - 10

    def __init__(self, name, size):
        self.type = name
        self.size = size
        

class GenericBusiness:
    #defines properties of a generic business
    money = 0
    inputRes = 0 #stock of input
    outputRes = 0 #stock of output
    equipQuality = 0 # 1 - 100
    matUse = 0 #material use per tick
    produce = 0 #amount produced per tick
    minSalePrice = 0 #minimum sale cost for output
    resourceProduced = Resc("tmp",-1) #resource produced by business
    potentialValue = 0

    def __init__(self, startMon, qual, resource):
        self.money = startMon
        self.equipQuality = qual
        self.minSalePrice = resource.price
        self.resourceProduced = resource

    def produceGoods(self):
        #determine the production for current tick
        self.determineProduction()
        #produce a tick
        self.inputRes = self.inputRes - self.matUse
        self.outputRes = self.outputRes + self.produce

        breakChance = random.randrange(0,9)
        if breakChance >= 7:
            self.equipQuality = self.equipQuality - 5
        self.potentialValue = self.minSalePrice * self.outputRes

    def determineProduction(self):
        #determines production as a function of equipQuality
        #probably will be modified for balance per specific business
        self.matUse = self.equipQuality / 2
        self.produce = self.equipQuality * 2

    def calcMinSalePrice(self):
        #calculate minimum sale price
        #uses volume in stock
        #sell in bulk = lower price
        if self.outputRes > 1000:
            self.minSalePrice = self.resourceProduced.price - random.randrange(0,15)
        elif self.outputRes > 500:
            self.minSalePrice = self.resourceProduced.price - random.randrange(0,8)
        else:
            self.minSalePrice = self.resourceProduced.price - random.randrange(0,5)
        print "New sale price: ", self.minSalePrice
        
    def repair(self):
        #repairs equip quality
        temp = 100 - self.equipQuality
        self.equipQuality = 100
        self.money = self.money - (5*temp)

    
class TierOneBusiness(GenericBusiness): #resource harvesting business, no input. only output.
    vein = RescVein(Resc("tmp",-1),-1)
    claimed = False

    def __init__(self, startMon, qual, vein):
        self.money = startMon
        self.equipQuality = qual
        self.minSalePrice = vein.type.price
        self.vein = vein
        self.resourceProduced = self.vein.type

    def determineProduction(self):
        #function of quality and vein size
        self.matUse = 0
        self.produce = (self.equipQuality * 2) + (self.vein.size * 5)
        print "New production: ", self.produce

class TierTwoBusiness(GenericBusiness): #manufacturing company
    resourceInput = Resc("tmp2", -2)    #TODO: add possiblity for multiple inputs
    supplier = TierOneBusiness(-1,-1,RescVein(Resc("tmp",-1),-1))
    hasSupplier = False
    
    def __init__(self,startMon,qual,resourceIn,resourceOut,suppList):
        GenericBusiness.__init__(startMon,qual,resourceOut)
        self.resourceInput = resourceIn
        self.hasSupplier = self.getSupplier(suppList)
        if self.hasSupplier == False:
            print "No supplier found, will run again at next produce cycle"
    
    def getSupplier(self,supplierList): #select supplier
        for potential in supplierList:
            if potential.vein.type == self.resourceInput and potential.produce >= self.matUse and potential.claimed == False:
                self.supplier = potenial
                potential.claimed = True
                return True
            else:
                return False
            
    def produceGoods(self,suppList):
        if self.hasSupplier == False:
            self.hasSupplier = self.getSupplier(suppList)
            return False
        if self.inputRes >= self.matUse:
            self.inputRes = self.inputRes - self.matUse
            self.outputRes += self.produce
        breakChance = random.randrange(0,10)
        if breakChance > 7:
            self.equipQuality -= 5
        self.potentialValue = (self.outputRes * self.minSalePrice) - (self.inputRes * resourceInput.price)
        return True